package com.newdjk.newdoctor.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.ui.LoginActivity;
import com.newdjk.okhttp.entity.BaseEntity;
import com.newdjk.okhttp.entity.DoctorInfoEntity;
import com.newdjk.okhttp.entity.Login;
import com.newdjk.okhttp.entity.MedicationCompanyEntity;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class LogOutUtil {
    private static final String TAG = "LogOutUtil";
    private BasActivity mActivity;

    public void loginOut(Context context, boolean b, final int type) {
        if (MyApplication.isLogin) {
            if (MyApplication.LoginEntity==null){

                return;
            }
            NetServices.Factory.getService().Logout(
                    MyApplication.LoginEntity.getToken() + ""
            ).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseComPleteObserver(context) {
                        @Override
                        protected void onSuccees(BaseEntity t) throws Exception {
                            Log.d(TAG, t.getData().toString());
                            if (type == 1) {
                                ToastUtil.setToast("token失效,请重新登录");
                            } else {
                                ToastUtil.setToast("退出登录成功");
                            }
                            MyApplication.DoctorInfoEntity = null;
                            MyApplication.medicationCompanyEntity = null;
                            MyApplication.LoginEntity = null;

                            SpUtils.put(Contants.Id, 0);
                            SpUtils.put(Contants.logoOut, true);
                            SpUtils.put(Contants.toLogin, "0");
                            SpUtils.put(Contants.LoginJson, "");
                            Intent intent = new Intent(context, LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK  | Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }

                        @Override
                        protected void onFailure(Throwable e, String erroressage, boolean isNetWorkError) throws Exception {
                            super.onFailure(e, erroressage, isNetWorkError);
                            if (type == 1) {
                                ToastUtil.setToast("token失效,请重新登录");
                            } else {
                                ToastUtil.setToast("退出登录成功");
                            }
                            MyApplication.DoctorInfoEntity = null;
                            MyApplication.medicationCompanyEntity = null;
                            MyApplication.LoginEntity = null;

                            SpUtils.put(Contants.Id, 0);
                            SpUtils.put(Contants.logoOut, true);
                            SpUtils.put(Contants.toLogin, "0");
                            SpUtils.put(Contants.LoginJson, "");
                            Intent intent = new Intent(context, LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK  | Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }
                    });

        }
    }


    private static class SingletonHolder {
        private static LogOutUtil instance = new LogOutUtil();
    }

    private LogOutUtil() {
    }

    public static LogOutUtil getInstance() {
        return SingletonHolder.instance;
    }


}
